<?php

namespace App\Services;

use App\Models\CustomerGroup;
use Slowlyo\OwlAdmin\Services\AdminService;

/**
 * 客户分组管理
 *
 * @method CustomerGroup getModel()
 * @method CustomerGroup|\Illuminate\Database\Query\Builder query()
 */
class CustomerGroupService extends AdminService
{
	protected string $modelName = CustomerGroup::class;

	/**
	 * 预加载关联关系
	 */
	public function listQuery()
	{
		$model = $this->getModel();
		return $this->query()
			->with(['company','operator','bd_uid'])
			->orderByDesc($model->getUpdatedAtColumn());
	}

	/**
	 * 获取处理后的列表数据
	 */
	public function list()
	{
		$list = parent::list();
		// 计算节点数量
		foreach ($list['items'] as &$row) {
			$row['node_name'] = $nodeNames = $row['node_name'] ? explode(',', $row['node_name']) : [];
			$row['nodecount'] = count($nodeNames);
		}
		return $list;
	}
}